package com.example.springdemo.domain.base;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * BaseResponse
 *
 * @author Carl
 * @date 2020.08.10 17:05
 */
@Data
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
public class BaseRes<T> implements Serializable {

    private static final long serialVersionUID = 3264318959908134167L;

    private int code;

    private String msg;

    private T data;

    public static <E> BaseRes<E> success(E data) {
        return new BaseRes<>(HttpStatus.OK.value(), "success", data);
    }

    public static <E> BaseRes<E> error(String msg) {
        return new BaseRes<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), msg, null);
    }

    public static <E> BaseRes<E> error(String msg, E data) {
        return new BaseRes<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), msg, data);
    }

    public static <E> BaseRes<E> error(int code, String msg, E data) {
        return new BaseRes<>(code, msg, data);
    }

    public static <E> BaseRes<E> badReq(String msg) {
        return new BaseRes<>(HttpStatus.BAD_REQUEST.value(), msg, null);
    }

}
